import 'package:flutter_base/config/app_config.dart';

class UtilsTime {
  // static String timestampToTimeString(int? timestamp) {
  //   if (timestamp == null) {
  //     return '';
  //   }
  //   // DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch)
  //   DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
  //   return '${dateTime.year}-${dateTime.month.toString().padLeft(2, '0')}-${dateTime.day.toString().padLeft(2, '0')}';
  // }

  static String timeCountDownByMs(int? ms, {String? defindStr}) {
    if (ms == null) {
      return '';
    }
    // int secondsTotal = (ms - UtilsTime.getServiceTimestamp());
    // if (secondsTotal <= 0) {
    //   return defindStr ?? '00:00:00';
    // }

    // 新增的时间显示规则
    DateTime now = DateTime.now();
    DateTime targetTime = DateTime.fromMillisecondsSinceEpoch(ms * 1000);
    Duration difference = now.difference(targetTime);

    if (difference.inMinutes < 1) {
      return '刚刚';
    } else if (difference.inHours < 1) {
      return '${difference.inMinutes}分钟前';
    } else if (difference.inDays == 0) {
      return '${difference.inHours}小时前';
    } else if (difference.inDays == 1) {
      return '昨天 ${targetTime.hour.toString().padLeft(2, '0')}:${targetTime.minute.toString().padLeft(2, '0')}';
    } else {
      return '${targetTime.year}-${targetTime.month.toString().padLeft(2, '0')}-${targetTime.day.toString().padLeft(2, '0')} ${targetTime.hour.toString().padLeft(2, '0')}:${targetTime.minute.toString().padLeft(2, '0')}';
    }
  }

  static String timeCountDownByMsForBackpack(int? ms, {String? defindStr}) {
    //背包显示 x天x时
    if (ms == null) {
      return '';
    }
    int secondsTotal = (ms - UtilsTime.getServiceTimestamp());

    if (secondsTotal <= 0) {
      return defindStr ?? '00:00';
    }
    // int days = secondsTotal ~/ (60 * 60 * 24);
    int hours = (secondsTotal ~/ 3600) % 24;
    int minutes = secondsTotal % 3600 ~/ 60;
    int seconds = secondsTotal % 60;

    if (hours > 0) {
     return '${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
    } else {
      return '${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
    }
  }

  static String timeCountDown(int? secondsTotal) {
    //背包显示 x天x时
    if (secondsTotal == null) {
      return '';
    }
    // int secondsTotal = (ms - UtilsTime.getServiceTimestamp());


    // int days = secondsTotal ~/ (60 * 60 * 24);
    int hours = (secondsTotal ~/ 3600) % 24;
    int minutes = secondsTotal % 3600 ~/ 60;
    int seconds = secondsTotal % 60;

    if (hours > 0) {
      return '${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
    } else {
      return '${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
    }
  }
  

  static String timeCountDownPkByMs(int? secondsTotal) {
    if (secondsTotal == null) {
      return '';
    }
    // int secondsTotal = ms ;
    // int secondsTotal = (ms - UtilsTime.getServiceTimestamp());
    if (secondsTotal <= 0) {
      return '00:00';
    }

    int minutes = secondsTotal % 3600 ~/ 60;
    int seconds = secondsTotal % 60;
    return '${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
  }

  static int getServiceTimestamp() {
    return (DateTime.now().millisecondsSinceEpoch ~/ 1000) +
        AppConfig.serviceDifference;
  }

  static void resetServiceTimestamp(int timeS) {
    AppConfig.serviceDifference =
        timeS - DateTime.now().millisecondsSinceEpoch ~/ 1000;
  }

  static String getYMDbySecond(int second) {
    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(second * 1000);
    String formattedDate = "${dateTime.year}-${dateTime.month}-${dateTime.day}";
    return formattedDate;
  }

  static int timeStringToSeconds(String timeString) {
    List<String> parts = timeString.split(':');
    if (parts.length != 3) {
      return 0;
    }
    int hours = int.parse(parts[0]);
    int minutes = int.parse(parts[1]);
    int seconds = int.parse(parts[2]);
    return hours * 3600 + minutes * 60 + seconds;
  }
}
